package com.cockybook.poemspider.job;

import com.cockybook.poemspider.beans.Poet;
import com.cockybook.poemspider.dao.PoetsRepository;
import com.cockybook.poemspider.dao.UrlBeanRepository;
import com.cockybook.utils.Const;
import com.cockybook.utils.JsoupUtil;
import com.cockybook.utils.StringUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.cockybook.utils.StringUtil.getAuthIntName;

/**
 * 抓取朝代
 */
//@Component
//@Order(1)
//@Deprecated
public class DynastySpider implements CommandLineRunner {

    private final Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    UrlBeanRepository urlBeanRepository;

    @Autowired
    PoetsRepository poetsRepository;

    Map<String ,String> dynastys =new HashMap<String, String>();

    int count=0;

    public DynastySpider(){
        this.dynastys.put("xianqinshiren","先秦");
        this.dynastys.put("hanchaoshiren","汉朝");
        this.dynastys.put("weijinshiren","魏晋");
        this.dynastys.put("nanbeichaoshiren","南北朝");
        this.dynastys.put("nanbeichaoshiren","隋朝");
        this.dynastys.put("tangdaishiren","唐朝");
        this.dynastys.put("tangdaishiren","宋朝");
        this.dynastys.put("tangdaishiren","金朝");
        this.dynastys.put("liaochaoshiren","辽朝");
        this.dynastys.put("yuanchaoshiren","元朝");
        this.dynastys.put("mingchaoshiren","明朝");
        this.dynastys.put("qingchaoshiren","清朝");
        this.dynastys.put("xiandangdaishiren","近当代");
    }


    @Override
    public void run(String... args) throws Exception {
        for (Map.Entry<String,String> entry:
             this.dynastys.entrySet()) {

            String url = String.format(Const.srcURLDynastyPREFINX, entry.getKey());
            logger.info("===============now Download dynasty "+ entry.getValue());
            count=0;
            parseDynasty(url,entry.getValue(),true);

        }

    }

    /**
     * 解析朝代url
     * @param dynastyUrl
     * @param isGodown  是否下钻
     */
    public void parseDynasty(String dynastyUrl,String dynastiy,boolean isGodown) throws Exception {
        Document document = JsoupUtil.urlGet(Const.srcURL+dynastyUrl);
        if(document!=null){
            logger.info(document.title());
            Elements links = document.getElementsByTag("a");

            for (Element link : links) {
                String linkHref = link.attr("href");
                String linkText = link.text();

                if(linkHref.startsWith("/chaxun/zuozhe") && linkHref.endsWith(".html")){
                    addPoet(link,dynastiy);
                }else if(linkHref.startsWith(dynastyUrl+"/page")) {//翻页
                    if (isGodown) {
                        parseDynasty(linkHref,dynastiy, false);
                    }
                }

            }

        }
    }


    /**
     * 添加一个诗人
     * @param link
     * @param dynastiy
     * @throws Exception
     */
    public void addPoet(Element link, String dynastiy) throws Exception {
        String linkHref = link.attr("href");
        Poet poet = new Poet();
        poet.setId(StringUtil.getAuthIntID(linkHref));
        poet.setName(getAuthIntName(link.text()));
        poet.setDynasty(dynastiy);
        if (! poetsRepository.exists(poet.getId())){
            poetsRepository.saveAndFlush(poet);
        }

        logger.info(String.format("Dynasty:%s , PoetName:%s ", dynastiy, poet.getName()));

    }




}
